#include "hRandom.h"
#include "hMath.h"

float hRandom::getRandom()
{
	return rand()/(float)RAND_MAX;
}
float hRandom::getRandom(float min, float max)
{
	return (rand()/(float)RAND_MAX * (max - min)) + min;
}

int hRandom::getRandomInt(int min, int max)
{
	int res = (int)floor((rand()/(float)RAND_MAX * (max - min + 1)) + min);
	// if rand() / RAND_MAX = 1 then the floor of value will be max+1...
	return res > max ? max : res;
}

const Vector2 hRandom::getRandomVector2()
{
	float angle = getRandom() * TwoPi;
	Vector2 v;
	v.x = cosf(angle);
	v.y = sinf(angle);
	return v;
}

const Vector3 hRandom::getRandomVector3()
{
	float z = 2* getRandom() -1; // range -1 to 1
	Vector2 planar = getRandomVector2() * sqrt(1-z*z);
	return Vector3(planar.x, planar.y, z);
}

const Vector3 hRandom::getRandomVector3(const Vector3 &variance)
{
	return Vector3(
		getRandom(-variance.x, variance.x),
		getRandom(-variance.y, variance.y),
		getRandom(-variance.z, variance.z));
}

const Vector3 hRandom::getRandomVector3(const Vector3 &varianceMin, const Vector3 &varianceMax)
{
	return Vector3(
		getRandom(varianceMin.x, varianceMax.x),
		getRandom(varianceMin.y, varianceMax.y),
		getRandom(varianceMin.z, varianceMax.z));
}

Vector4 hRandom::getRandomVector4(const Vector4 &varianceMin, const Vector4 &varianceMax)
{
	return Vector4(
                   getRandom(varianceMin.x, varianceMax.x),
                   getRandom(varianceMin.y, varianceMax.y),
                   getRandom(varianceMin.z, varianceMax.z),
                   getRandom(varianceMin.w, varianceMax.w));
}